﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Vision.VControl;

namespace Vision.VHelper
{
    public class ConnectionHelper
    {
        public Guid ConnectionId { get; } = Guid.NewGuid();
        public FlowNode StartNode { get; set; }
        public FlowNode EndNode { get; set; }
        public LineForward Direction;
        public Path PathElement { get; set; }
        public Point StartPoint { get; private set; }
        public Point EndPoint { get; private set; }
        public Canvas Canvas { get; set; }

        public ConnectionHelper(FlowNode startNode, FlowNode endNode, Canvas canvas)
        {
            StartNode = startNode;
            EndNode = endNode;
            Canvas = canvas;
            PathElement = new Path
            {
                Stroke = Brushes.DarkRed,
                StrokeThickness = 5,
                StrokeLineJoin = PenLineJoin.Round
            };
            DrawPointToPointLine();
        }

        public void DrawPointToPointLine()
        {
            // 获取控件初始位置
            var StartNodePoint = new Point(Canvas.GetLeft(StartNode), Canvas.GetTop(StartNode));
            var EndNodePoint = new Point(Canvas.GetLeft(EndNode), Canvas.GetTop(EndNode));

            if (Math.Abs(EndNodePoint.X - StartNodePoint.X) > Math.Abs(EndNodePoint.Y - StartNodePoint.Y) && EndNodePoint.X >= StartNodePoint.X)
            {
                StartPoint = new Point(StartNodePoint.X + StartNode.RenderSize.Width + 1, StartNodePoint.Y + StartNode.RenderSize.Height / 2);
                EndPoint = new Point(EndNodePoint.X - 1, EndNodePoint.Y + EndNode.RenderSize.Height / 2);
                DrawJoinLine(StartPoint, EndPoint, LineForward.L_R);
            }
            else if (Math.Abs(EndNodePoint.X - StartNodePoint.X) > Math.Abs(EndNodePoint.Y - StartNodePoint.Y) && EndNodePoint.X < StartNodePoint.X)
            {
                StartPoint = new Point(StartNodePoint.X - 1, StartNodePoint.Y + StartNode.RenderSize.Height / 2);
                EndPoint = new Point(EndNodePoint.X + EndNode.RenderSize.Width + 1, EndNodePoint.Y + EndNode.RenderSize.Height / 2);
                DrawJoinLine(StartPoint, EndPoint, LineForward.R_L);
            }
            else if (EndNodePoint.Y >= StartNodePoint.Y)
            {
                StartPoint = new Point(StartNodePoint.X + StartNode.RenderSize.Width / 2, StartNodePoint.Y + StartNode.RenderSize.Height + 1);
                EndPoint = new Point(EndNodePoint.X + EndNode.RenderSize.Width / 2, EndNodePoint.Y - 1);
                DrawJoinLine(StartPoint, EndPoint, LineForward.U_D);
            }
            else
            {
                StartPoint = new Point(StartNodePoint.X + StartNode.RenderSize.Width / 2, StartNodePoint.Y - 1);
                EndPoint = new Point(EndNodePoint.X + EndNode.RenderSize.Width / 2, EndNodePoint.Y + EndNode.RenderSize.Height + 1);
                DrawJoinLine(StartPoint, EndPoint, LineForward.D_U);
            }
        }

        private void DrawJoinLine(Point p1, Point p2, LineForward forward)
        {

            Point inflectPoint1;
            Point inflectPoint2;
            Point arrowTip;
            Point arrowLeft;
            Point arrowRight;

            if (forward == LineForward.L_R)
            {
                inflectPoint1 = new Point((p1.X + p2.X) / 2, p1.Y);
                inflectPoint2 = new Point((p1.X + p2.X) / 2, p2.Y);
                // 计算箭头尖端和翼的位置
                arrowTip = new Point(p2.X, p2.Y + 10 * (p2.Y - inflectPoint2.Y)); // 箭头尖端向右移动10个单位
                arrowLeft = new Point(p2.X - 5, p2.Y - 5); // 箭头左侧翼
                arrowRight = new Point(p2.X - 5, p2.Y + 5); // 箭头右侧翼
            }
            else if (forward == LineForward.R_L)
            {
                inflectPoint1 = new Point((p1.X + p2.X) / 2, p1.Y);
                inflectPoint2 = new Point((p1.X + p2.X) / 2, p2.Y);
                // 计算箭头尖端和翼的位置
                arrowTip = new Point(p2.X, p2.Y + 10 * (p2.Y - inflectPoint2.Y)); // 箭头尖端向右移动10个单位
                arrowLeft = new Point(p2.X + 5, p2.Y - 5); // 箭头左侧翼
                arrowRight = new Point(p2.X + 5, p2.Y + 5); // 箭头右侧翼
            }
            else if (forward == LineForward.U_D)
            {
                inflectPoint1 = new Point(p1.X, (p1.Y + p2.Y) / 2);
                inflectPoint2 = new Point(p2.X, (p1.Y + p2.Y) / 2);
                // 计算箭头尖端和翼的位置
                arrowTip = new Point(p2.X + 10 * (p2.X - inflectPoint2.X), p2.Y); // 箭头尖端向右移动10个单位
                arrowLeft = new Point(p2.X - 5, p2.Y - 5); // 箭头左侧翼
                arrowRight = new Point(p2.X + 5, p2.Y - 5); // 箭头右侧翼
            }
            else if (forward == LineForward.D_U)
            {
                inflectPoint1 = new Point(p1.X, (p1.Y + p2.Y) / 2);
                inflectPoint2 = new Point(p2.X, (p1.Y + p2.Y) / 2);
                // 计算箭头尖端和翼的位置
                arrowTip = new Point(p2.X + 10 * (p2.X - inflectPoint2.X), p2.Y); // 箭头尖端向右移动10个单位
                arrowLeft = new Point(p2.X - 5, p2.Y + 5); // 箭头左侧翼
                arrowRight = new Point(p2.X + 5, p2.Y + 5); // 箭头右侧翼
            }


            // 根据方向绘制Path
            var pathGeometry = new PathGeometry();
            var pathFigure = new PathFigure { StartPoint = p1 };
            pathFigure.Segments.Add(new LineSegment { Point = inflectPoint1 });
            pathFigure.Segments.Add(new LineSegment { Point = inflectPoint2 });
            pathFigure.Segments.Add(new LineSegment { Point = p2 });

            // 添加箭头尖端和翼
            pathFigure.Segments.Add(new LineSegment { Point = arrowTip });
            pathFigure.Segments.Add(new LineSegment { Point = arrowLeft });
            pathFigure.Segments.Add(new LineSegment { Point = p2 });
            pathFigure.Segments.Add(new LineSegment { Point = arrowRight });
            pathFigure.Segments.Add(new LineSegment { Point = p2 });

            pathGeometry.Figures.Add(pathFigure);
            PathElement.Data = pathGeometry;

            // 如果PathElement还没有被添加到Canvas，需要添加
            if (PathElement.Parent == null)
            {
                Canvas.Children.Add(PathElement);
            }

        }
    }

    /// <summary>
    /// 连线状态
    /// </summary>
    public enum DrawState
    {
        Normal, // 正常状态
        DrawLine // 连线状态
    }

    /// <summary>
    /// 连线方向
    /// </summary>
    public enum LineForward
    {
        L_R, // 左到右
        R_L, // 右到左
        U_D, // 上到下
        D_U // 下到上
    }
}
